package main

import (
	"fmt"
	"html/template"
	"net/http"
	"os"
)

type Result struct {
	Text  string
	Image string
}

func survey(w http.ResponseWriter, r *http.Request) {
	vars := r.URL.Query()
	dog, ok := vars["dog"]
	if ok {
		var res Result
		if dog[0] == "不养" {
			res = Result{
				Text:  "这就说明，你是个gay",
				Image: "why.jpg",
			}
		} else if dog[0] == "养" {
			res = Result{
				Text:  "好吧，那祝您阖家欢乐",
				Image: "homo.png",
			}
		} else {
			res = Result{
				Text:  "别乱写了，谁又不是gay呢",
				Image: "Im.gif",
			}
		}
		tmpl, err := template.ParseFiles("result.tmpl")
		if err != nil {
			fmt.Println(err)
			os.Exit(2)
		}
		tmpl.Execute(w, res)
	} else {
		tmpl, err := template.ParseFiles("index.tmpl")
		if err != nil {
			fmt.Println(err)
			os.Exit(2)
		}
		tmpl.Execute(w, nil)
	}
}

func main() {
	http.Handle("/assets/js/", http.StripPrefix("/assets/js/", http.FileServer(http.Dir("assets/js/"))))
	http.Handle("/assets/images/", http.StripPrefix("/assets/images/", http.FileServer(http.Dir("assets/images/"))))
	http.HandleFunc("/", survey)
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}
}
